package com.yunfangdata.web.controller;


import com.yunfangdata.domain.Role;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IRoleService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequiresPermissions("role:index")
    @PermissionName("角色首页")
    @RequestMapping("")
    public String index(){
        return "role/list";
    }

    @RequiresPermissions("role:list")
    @PermissionName("角色列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return roleService.query(qo);
    }

    @RequiresPermissions("role:save")
    @PermissionName("角色保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Role role){
        JsonResult result = new JsonResult();
        try {
            roleService.save(role);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;

    }

    @RequiresPermissions("role:update")
    @PermissionName("角色更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Role role){
        JsonResult result = new JsonResult();
        try {
            roleService.update(role);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;

    }

    @RequiresPermissions("role:delete")
    @PermissionName("角色删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            roleService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("删除失败");
        }
        return result;

    }


    @RequestMapping("roles")
    @ResponseBody
    public List<Role> roles(){
        return roleService.list();
    }


    @RequestMapping("queryEmpForRoles")
    @ResponseBody
    public List<Role> queryEmpForRoles(Long empId){
        return roleService.queryEmpForRoles(empId);
    }



}
